<template>
  <div class="app-container">
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="postForm"
      label-position="right"
      label-width="100px"
      style="width: 600px; margin-left:50px;"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item
            label="appid"
            prop="appid"
          >
            <el-input v-model="postForm.appid" clearable placeholder="appid" maxlength="20" disabled="disabled" />
          </el-form-item>
        </el-col>
        <el-col :span="12" />
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="应用名称" prop="appName">
            <el-input v-model="postForm.appName" placeholder="应用名称" maxlength="20" :disabled="formDisabled" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="授权类型" prop="authType">
            <el-select
              v-model="postForm.authType"
              placeholder="请选择"
              style="width: 100%;"
              :disabled="formDisabled"
            >
              <el-option
                v-for="item in authTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="ip" prop="ip">
        <el-input v-model="postForm.ip" placeholder="ip范围" maxlength="500" :disabled="formDisabled" />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="联系人" prop="contacts">
            <el-input v-model="postForm.contacts" placeholder="联系人" maxlength="10" :disabled="formDisabled" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系方式" prop="mobile">
            <el-input v-model="postForm.mobile" placeholder="联系方式" maxlength="20" :disabled="formDisabled" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="是否禁用" prop="status">
        <el-select
          v-model="postForm.status"
          filterable
          placeholder="请选择"
          style="width: 100%;"
          :disabled="formDisabled"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="有效期限" prop="validDate">
        <el-date-picker
          v-model="postForm.validDate"
          type="date"
          placeholder="有效期限"
          style="width: 140px;"
          value-format="yyyy-MM-dd"
          :disabled="formDisabled"
        />
      </el-form-item>
      <el-form-item label="权限">
        <el-tree
          ref="tree"
          :data="apiData"
          :props="defaultProps"
          show-checkbox
          node-key="id"
          :default-checked-keys="defaultCheckedKeys"
          class="permission-tree"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="postForm.remarks" placeholder="备注" maxlength="200" :disabled="formDisabled" />
      </el-form-item>
      <el-form-item>
        <el-button @click="back()">返回</el-button>
        <el-button v-if="submitHidden===false" type="primary" @click="submitForm">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addSysApp, updateSysApp, getSysApp } from '@/api/sys/app'
import msgUtil from '@/utils/msgUtil'
import routerUtil from '@/utils/routerUtil'
import { getApiTree } from '@/api/common/common'
import ComboConst from '@/utils/ComboConst'

const defaultForm = {
  appid: null,
  appsecrect: null,
  authType: 2,
  apiData: [],
  apiIds: null,
  name: null,
  ip: null,
  contacts: null,
  mobile: null,
  remarks: null,
  validDate: null,
  disabled: 1
}

// 处理返回的资源数据生成资源选择树
export function filterApiTree(apis) {
  const res = []
  apis.forEach(api => {
    const tmp = {}
    tmp.id = api.id
    tmp.label = api.name
    if (api.children) {
      tmp.children = filterApiTree(api.children)
    }
    // tmp.checked = true
    res.push(tmp)
  })
  return res
}

export default {
  name: 'SysAppDetail',
  props: {
    operaType: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      apiData: [],
      postForm: Object.assign({}, defaultForm),
      formDisabled: false, // 表单是否禁用
      submitHidden: false, // 提交按钮是否隐藏
      statusOptions: ComboConst.statusOptions,
      authTypeOptions: ComboConst.authTypeOptions,
      defaultCheckedKeys: null,
      rules: {
        appName: [{ required: true, trigger: 'blur' }
        ],
        authType: [{ required: true, trigger: 'blur' }
        ],
        contacts: [{ required: true, trigger: 'blur' },
          { min: 1, max: 10, trigger: 'change' }
        ],
        mobile: [
          { required: true, trigger: 'blur' },
          { min: 1, max: 11, trigger: 'change' }
        ],
        disabled: [
          { required: true, trigger: 'blur' }
        ], validDate: [
          { required: true, trigger: 'blur' }
        ]
      },
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  created() {
    if (this.operaType === 2) {
      this.getSysApp(this.$route.query.appid)
    } else if (this.operaType === 3) {
      this.getSysApp(this.$route.query.appid)
      this.formDisabled = true
      this.submitHidden = true
    } else {
      this.loading = false
    }
    this.getApiTree()
  },
  methods: {
    async getApiTree() {
      getApiTree({}).then(response => {
        this.apiData = filterApiTree(response.data)
        /* console.log(this.apiIds)
        if (this.apiIds !== null) {
          this.defaultCheckedKeys = this.apiIds.split('@')
        }*/
      })
    },

    // 修改app
    updateSysApp() {
      this.postForm.apiIds = this.$refs.tree.getCheckedKeys().join('@').concat('@').concat(this.$refs.tree.getHalfCheckedKeys().join('@'))
      updateSysApp(this.postForm).then(response => {
        msgUtil.success()
        routerUtil.backPath(this, '/sys/app/index')
      })
    },
    // 新增用户
    addSysApp() {
      this.postForm.apiIds = this.$refs.tree.getCheckedKeys().join('@').concat('@').concat(this.$refs.tree.getHalfCheckedKeys().join('@'))
      addSysApp(this.postForm).then(response => {
        msgUtil.success()
        routerUtil.backPath(this, '/sys/app/index')
      })
    },
    // 返回
    back() {
      routerUtil.backPath(this, '/sys/app/index')
    },
    // 提交form
    submitForm() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (this.operaType === 2) {
            this.updateSysApp()
          } else if (this.operaType === 1) {
            this.addSysApp()
          }
        }
      })
    },
    // 获取角色信息并赋值给当前页面
    getSysApp(appid) {
      var json = { 'appid': appid }
      getSysApp(json).then(response => {
        this.postForm = response.data
        this.apiIds = response.data.apiIds
        if (this.apiIds !== null) {
          this.defaultCheckedKeys = this.apiIds.split('@')
        }
      })
    }
  }
}
</script>
